﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using DHQGHN.DAL.BO.DanhMuc;

namespace DHQGHN.Services.DanhMuc
{
    public class AdministrativeUnitExtService : IAdministrativeUnitExtService
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(AdministrativeUnitExtService));

        private AdministrativeUnitExtBO administrativeUnitExtBO;

        private AdministrativeUnitBO administrativeUnitBO;

        #region Constructors
        /// <summary>
        /// 
        /// </summary>
        public AdministrativeUnitExtService()
        {
            //string connString = ConfigurationManager.ConnectionStrings["DHQGHN_TDT.ConnectionString"].ConnectionString;
            administrativeUnitExtBO = new AdministrativeUnitExtBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
            administrativeUnitBO = new AdministrativeUnitBO(DHQGHN.DAL.Common.ConnectionUtil.TKTHDS_TDT_CONNECTION_STRING);
        }
        #endregion

        #region Service Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listName"></param>
        /// <returns></returns>
        public IEnumerable<DAL.Models.DanhMuc.ADM_UNIT_EXT> GetAll()
        {
            try
            {
                return administrativeUnitExtBO.GetAll();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public IEnumerable<DAL.Models.DanhMuc.Template> GetNameByAreaID(string AreaID, string type)
        {
            try
            {
                if (DHQGHN.Common.Utils.IsNullOrEmptyOverride(AreaID) ||
                    DHQGHN.Common.Utils.IsNullOrEmptyOverride(type))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                if ("1".Equals(type)) 
                { 
                    return administrativeUnitExtBO.GetName(AreaID);
                }
                else if ("2".Equals(type))
                {
                    return administrativeUnitBO.GetADMDISTRICTPROVINCEName(AreaID);
                }
                return null;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }

        public IEnumerable<DAL.Models.DanhMuc.Template> GetNameByAdmUnitExtID(string AdmUnitExtID, string Levels)
        {
            try
            {
                if (DHQGHN.Common.Utils.IsNullOrEmptyOverride(AdmUnitExtID))
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Thiếu tham số",
                        Description = "Thiếu tham số"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }

                int lv = -1;
                if (int.TryParse(Levels, out lv) == false)
                {
                    Fault.ValidationFault fault = new Fault.ValidationFault()
                    {
                        Result = false,
                        Message = "Tham số Levels sai định dạng!",
                        Description = "Tham số Levels sai định dạng!"
                    };

                    throw new FaultException<Fault.ValidationFault>(fault);
                }
                return administrativeUnitExtBO.GetName(AdmUnitExtID, lv);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw new FaultException<Fault.UnexpectedServiceFault>(
                    new Fault.UnexpectedServiceFault
                    {
                        ErrorMessage = ex.Message,
                        Source = ex.Source,
                        Target = ex.TargetSite.ToString(),
                        StackTrace = ex.StackTrace
                    },
                    new FaultReason(string.Format("{0}", "Service fault exception")));
            }
        }
        #endregion
    }
}